package com.finance.cooperate.strategy.core.common;

import io.vavr.Tuple;
import io.vavr.Tuple2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName Builder
 * @Description 规则构造器
 * @Author shenanxin
 * @Date 2023/8/23 14:36
 * @Modify ...
 */
@SuppressWarnings("all")
public abstract class Builder<C extends BaseHandler> {

    protected ThreadLocal<List<Tuple2<Relationship, List<C>>>> processors = ThreadLocal.withInitial(() -> new ArrayList<>());

    public Builder join() {
        return this;
    }

    /**
     * @Author shen
     * @Description 表示所有命中才处理
     * @Date 5:46 下午 2020/12/8
     * @Param [handler]
     * @return cm.finance.crm.strategy_info.core.Builder
     **/
    public Builder and(C... handler) {

        this.processors.get().add(
                Tuple.of(Relationship.and, Arrays.asList(handler))
        );

        return this;
    }



    /**
     * @Author shen
     * @Description 任意一个命中则处理
     * @Date 3:02 下午 2021/5/11
     * @Param [handler]
     * @return cm.finance.crm.strategy.core.common.Builder
     **/
    public Builder or(C... handler) {
        this.processors.get().add(
                Tuple.of(Relationship.or, Arrays.asList(handler))
        );
        return this;
    }

    /**
     * @Author shen
     * @Description 分组处理（AB实验流量）
     * @Date 3:23 下午 2021/5/12
     * @Param [handler]
     * @return cm.finance.crm.strategy.core.common.Builder
     **/
    public Builder group(C... handler) {
        this.processors.get().add(
                Tuple.of(Relationship.group, Arrays.asList(handler))
        );
        return this;
    }


    public void clear() {
        this.processors.get().clear();
    }

    public abstract void builder();

}
